﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BasicFramework.Components;
using SlimDX.DirectInput;
using SlimDX.Multimedia;
using SlimDX.RawInput;

namespace BasicFrameworkComponentLibrary.GeneralComponents.InputComponents
{
    public class InputReceiver : Component
    {

        #region Fields

        private DirectInput input;
        private Keyboard keyboard;

        #endregion


        #region Properties

        #endregion


        #region Constructors

        public override void Initialize(bool createDependencies)
        {
            base.Initialize(createDependencies);
            input = new DirectInput();
            keyboard = new Keyboard(input);



        }

        void Device_RawInput(object sender, RawInputEventArgs e)
        {
            if (this.RawInput != null)
            {
                this.RawInput(this, e);
            }
        }

        void Device_KeyboardInput(object sender, KeyboardInputEventArgs e)
        {
            if (this.KeyboardInput != null)
            {
                this.KeyboardInput(this, e);
            }
        }

        void Device_MouseInput(object sender, MouseInputEventArgs e)
        {
            if (this.MouseInput != null)
            {
                this.MouseInput(this, e);
            }
        }


        public override void LoadResources(bool loadAllContent)
        {
            base.LoadResources(loadAllContent);

            SlimDX.RawInput.Device.RegisterDevice(UsagePage.Generic, UsageId.Mouse, SlimDX.RawInput.DeviceFlags.None);
            SlimDX.RawInput.Device.RegisterDevice(UsagePage.Generic, UsageId.Keyboard, SlimDX.RawInput.DeviceFlags.None);
            SlimDX.RawInput.Device.RegisterDevice(UsagePage.Generic, UsageId.Gamepad, SlimDX.RawInput.DeviceFlags.None);
            SlimDX.RawInput.Device.RegisterDevice(UsagePage.Generic, UsageId.Joystick, SlimDX.RawInput.DeviceFlags.None);

            SlimDX.RawInput.Device.MouseInput += new EventHandler<MouseInputEventArgs>(Device_MouseInput);
            SlimDX.RawInput.Device.KeyboardInput += new EventHandler<KeyboardInputEventArgs>(Device_KeyboardInput);
            SlimDX.RawInput.Device.RawInput += new EventHandler<RawInputEventArgs>(Device_RawInput);
        }
        public override void UnloadResources(bool unloadAllContent)
        {
            base.UnloadResources(unloadAllContent);
            input.Dispose();
            keyboard.Dispose();
            SlimDX.RawInput.Device.MouseInput -= new EventHandler<MouseInputEventArgs>(Device_MouseInput);
            SlimDX.RawInput.Device.KeyboardInput -= new EventHandler<KeyboardInputEventArgs>(Device_KeyboardInput);
            SlimDX.RawInput.Device.RawInput -= new EventHandler<RawInputEventArgs>(Device_RawInput);
        }

        #endregion


        #region Events

        public event EventHandler<MouseInputEventArgs> MouseInput;
        public event EventHandler<KeyboardInputEventArgs> KeyboardInput;
        public event EventHandler<RawInputEventArgs> RawInput;

        #endregion


        #region Methods

        #endregion

    }
}
